package com.ivan.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.common.result.Result;
import com.ivan.model.entity.hosp.Schedule;
import com.ivan.hospital.service.ScheduleService;
import com.ivan.model.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
 * @Author      Ivan
 * @Date        2023/4/4 21:05
 * @Description
 */
@Api(tags = "医院排班管理接口")
//@CrossOrigin//使用gateway后做了配置
@RestController
@RequestMapping("/admin/hosp/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "条件查询医院排班带分页")
    @GetMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") long current,
                             @PathVariable("limit") long limit,
                             ScheduleQueryVo queryVo) {
        Page<Schedule> page = scheduleService.findByPage(current, limit, queryVo);
        return Result.ok(page);
    }

    @ApiOperation(value = "根据id查询医院排班")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Long id) {
        Schedule schedule = scheduleService.findById(id);
        return Result.ok(schedule);
    }

    @ApiOperation(value = "添加医院排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(@RequestBody Schedule schedule) {
        int save = scheduleService.saveSchedule(schedule);
        if (save > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id删除医院排班")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        int delete = scheduleService.removeById(id);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据idList批量删除医院排班")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> idList) {
        int delete = scheduleService.removeByIds(idList);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "修改医院排班")
    @PostMapping("/updateSchedule")
    public Result updateSchedule(@RequestBody Schedule schedule) {
        int update = scheduleService.updateSchedule(schedule);
        if (update > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }





    @ApiOperation(value ="根据医院编号和科室编号,查询排班规则数据")
    @GetMapping("getScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result getScheduleRule(@PathVariable long page,
                                  @PathVariable long limit,
                                  @PathVariable String hoscode,
                                  @PathVariable String depcode) {
        Map<String,Object> map = scheduleService.getBookingScheduleRule(page,limit,hoscode,depcode);
        return Result.ok(map);
    }

    @ApiOperation(value ="根据医院编号,科室编号和工作日期,查询排班详细信息")
    @GetMapping("getScheduleDetail/{hoscode}/{depcode}/{workDate}")
    public Result getScheduleDetail(@PathVariable String hoscode,
                                    @PathVariable String depcode,
                                    @PathVariable String workDate) {
        List<Schedule> scheduleList = scheduleService.getScheduleDetail(hoscode, depcode, workDate);
        return Result.ok(scheduleList);
    }
}
